#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
创建数据库脚本
"""

import mysql.connector
from mysql.connector import Error

def create_database():
    """创建fastapi数据库"""
    try:
        # 连接MySQL服务器（不指定数据库）
        connection = mysql.connector.connect(
            host='localhost',
            port=3306,
            user='root',
            password='123456'
        )
        
        if connection.is_connected():
            cursor = connection.cursor()
            
            # 创建数据库
            create_db_query = "CREATE DATABASE IF NOT EXISTS fastapi CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
            cursor.execute(create_db_query)
            
            print("✅ 数据库 'fastapi' 创建成功或已存在")
            
            # 显示所有数据库
            cursor.execute("SHOW DATABASES")
            databases = cursor.fetchall()
            print("📋 当前所有数据库:")
            for db in databases:
                print(f"  - {db[0]}")
            
            cursor.close()
            connection.close()
            
    except Error as e:
        print(f"❌ 创建数据库失败: {e}")

if __name__ == "__main__":
    create_database() 